package com.viish.apps.tripletriad;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.TextView;
import android.widget.Toast;

import com.viish.apps.tripletriad.carte.DatabaseStream;
import com.viish.apps.tripletriad.carte.MyCards;
import com.viish.apps.tripletriad.carte.MyDecks;
import com.viish.apps.tripletriad.carte.Shop;

//TODO : am�liorer animations
public class TripleTriad extends Activity implements OnClickListener 
{
	public static final int ANIMATION_DURATION_SHORT = 500;
	public static final int ANIMATION_DURATION_LONG = 500;
	public static final int MAIN_MENU = 0;
	public static final int GAME_MENU = 1;
	public static final int PVP_MENU = 2;
	public static final int TEXT_SIZE = 50;
	public static final String VERSION = "v1";
	
	private TextView play, cards, decks, shop, about, playerVsBot, playerVsPlayer, botVsBot, pvpServeur, pvpClient, back;
	private int menu = MAIN_MENU;
	
    @Override
	public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        
        Typeface typeface = Typeface.createFromAsset(getAssets(), "ff8font.ttf");
        typeface = Typeface.create(typeface, Typeface.BOLD);
		play = (TextView) findViewById(R.id.bPlay);
		play.setOnClickListener(this);
		play.setTextColor(Color.BLACK);
		play.setTypeface(typeface);
		play.setTextSize(TEXT_SIZE);
		shop = (TextView) findViewById(R.id.bBuy);
		shop.setOnClickListener(this);
		shop.setTextColor(Color.BLACK);
		shop.setTypeface(typeface);
		shop.setTextSize(TEXT_SIZE);
		cards = (TextView) findViewById(R.id.bMyCards);
		cards.setOnClickListener(this);
		cards.setTextColor(Color.BLACK);
		cards.setTypeface(typeface);
		cards.setTextSize(TEXT_SIZE);
		decks = (TextView) findViewById(R.id.bMyDecks);
		decks.setOnClickListener(this);
		decks.setTextColor(Color.BLACK);
		decks.setTypeface(typeface);
		decks.setTextSize(TEXT_SIZE);
		about = (TextView) findViewById(R.id.bAbout);
		about.setOnClickListener(this);
		about.setTextColor(Color.BLACK);
		about.setTypeface(typeface);
		about.setTextSize(TEXT_SIZE);
		

		playerVsBot = (TextView) findViewById(R.id.bPvE);
		playerVsBot.setOnClickListener(this);
		playerVsBot.setTextColor(Color.BLACK);
		playerVsBot.setTypeface(typeface);
		playerVsBot.setTextSize(TEXT_SIZE);
		playerVsPlayer = (TextView) findViewById(R.id.bPvP);
		playerVsPlayer.setOnClickListener(this);
		playerVsPlayer.setTextColor(Color.BLACK);
		playerVsPlayer.setTypeface(typeface);
		playerVsPlayer.setTextSize(TEXT_SIZE);
		botVsBot = (TextView) findViewById(R.id.bEvE);
		botVsBot.setOnClickListener(this);
		botVsBot.setTextColor(Color.BLACK);
		botVsBot.setTypeface(typeface);
		botVsBot.setTextSize(TEXT_SIZE);
		
		pvpServeur = (TextView) findViewById(R.id.bPvPS);
		pvpServeur.setOnClickListener(this);
		pvpServeur.setTextColor(Color.BLACK);
		pvpServeur.setTypeface(typeface);
		pvpServeur.setTextSize(TEXT_SIZE);
		pvpClient = (TextView) findViewById(R.id.bPvPC);
		pvpClient.setOnClickListener(this);
		pvpClient.setTextColor(Color.BLACK);
		pvpClient.setTypeface(typeface);
		pvpClient.setTextSize(TEXT_SIZE);

		back = (TextView) findViewById(R.id.bBack);
		back.setOnClickListener(this);
		back.setTextColor(Color.BLACK);
		back.setTypeface(typeface);
		back.setTextSize(TEXT_SIZE);
		
		DatabaseStream dbs = new DatabaseStream(this);        
		if (dbs.isFirstTime())
		{
			dbs.initCards();
			Toast.makeText(this, getString(R.string.newcards), Toast.LENGTH_LONG).show();
		}
		int gils = dbs.getGils();
        dbs.close();
        
		TextView tvGils = (TextView) findViewById(R.id.tvGils);
		tvGils.setText(gils + " Gils");
		tvGils.setTextColor(Color.BLACK);
		tvGils.setTextSize(TEXT_SIZE);
		tvGils.setTypeface(typeface);
    }
    
    @Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
    	DatabaseStream dbs = new DatabaseStream(this);      
    	int gils = dbs.getGils();
        dbs.close();
    	TextView tvGils = (TextView) findViewById(R.id.tvGils);
		tvGils.setText(gils + " Gils");
    }

    private void showAbout()
    {
    	AlertDialog.Builder alert = new AlertDialog.Builder(this);  

		alert.setTitle("Triple Triad " + VERSION);
		alert.setMessage(getString(R.string.welcome));
		alert.setInverseBackgroundForced(true);
		alert.setIcon(R.drawable.pampa);
		 
		alert.setNegativeButton(getString(R.string.button_back), null);
		alert.setNeutralButton(getString(R.string.button_news), new DialogInterface.OnClickListener()
		{
			@Override
			public void onClick(DialogInterface arg0, int arg1) 
			{
				String url = "http://www.thinkdroid.eu";
				Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
				startActivity(myIntent); 
			}
		});
		
		alert.show();
    }
    
    public Animation fadeOutAnimation(final View v)
    {
    	Animation fadeOut = new AlphaAnimation(1, 0);
		fadeOut.setDuration(ANIMATION_DURATION_SHORT);
		fadeOut.setAnimationListener(new AnimationListener() 
		{
			public void onAnimationStart(Animation animation) { }
			public void onAnimationRepeat(Animation animation) { }
			
			public void onAnimationEnd(Animation animation) 
			{
				v.setVisibility(View.INVISIBLE);
			}
		});
		return fadeOut;
    }
    
    public Animation fadeInAnimation(final View v)
    {
    	Animation fadeIn = new AlphaAnimation(0, 1);
		fadeIn.setDuration(ANIMATION_DURATION_LONG);
		fadeIn.setAnimationListener(new AnimationListener() 
		{
			public void onAnimationStart(Animation animation) { }
			public void onAnimationRepeat(Animation animation) { }
			
			public void onAnimationEnd(Animation animation)
			{ 
				v.setVisibility(View.VISIBLE);
			}
		});
		
		return fadeIn;
    }
    

	public boolean onKeyDown(int keyCode, KeyEvent event)
    {
    	if (keyCode == KeyEvent.KEYCODE_BACK && menu == GAME_MENU)
    	{
    		playerVsBot.startAnimation(fadeOutAnimation(playerVsBot));
    		playerVsPlayer.startAnimation(fadeOutAnimation(playerVsPlayer));
    		botVsBot.startAnimation(fadeOutAnimation(botVsBot));
    		back.startAnimation(fadeOutAnimation(back));
    		
			play.startAnimation(fadeInAnimation(play));
			shop.startAnimation(fadeInAnimation(shop));
			cards.startAnimation(fadeInAnimation(cards));
			decks.startAnimation(fadeInAnimation(decks));
			about.startAnimation(fadeInAnimation(about));
			menu = MAIN_MENU;
			
			return true;
    	}
    	else if (keyCode == KeyEvent.KEYCODE_BACK && menu == PVP_MENU)
    	{
    		pvpServeur.startAnimation(fadeOutAnimation(pvpServeur));
			pvpClient.startAnimation(fadeOutAnimation(pvpClient));
    		
    		playerVsBot.startAnimation(fadeInAnimation(playerVsBot));
    		playerVsPlayer.startAnimation(fadeInAnimation(playerVsPlayer));
    		botVsBot.startAnimation(fadeInAnimation(botVsBot));
			menu = GAME_MENU;
			
			return true;
    	}
    	return super.onKeyDown(keyCode, event);
    }
    
	public void onClick(View v) 
	{
		if (v.equals(back))
		{
			onKeyDown(KeyEvent.KEYCODE_BACK, null);
		}
		if (v.equals(play))
		{
			play.startAnimation(fadeOutAnimation(play));
			shop.startAnimation(fadeOutAnimation(shop));
			cards.startAnimation(fadeOutAnimation(cards));
			decks.startAnimation(fadeOutAnimation(decks));
			about.startAnimation(fadeOutAnimation(about));
			
			playerVsBot.startAnimation(fadeInAnimation(playerVsBot));
			playerVsPlayer.startAnimation(fadeInAnimation(playerVsPlayer));
			botVsBot.startAnimation(fadeInAnimation(botVsBot));
			back.startAnimation(fadeInAnimation(back));
			menu = GAME_MENU;
		}
		else if (v.equals(playerVsPlayer))
		{
			playerVsBot.startAnimation(fadeOutAnimation(playerVsBot));
			playerVsPlayer.startAnimation(fadeOutAnimation(playerVsPlayer));
			botVsBot.startAnimation(fadeOutAnimation(botVsBot));
			
			pvpServeur.startAnimation(fadeInAnimation(pvpServeur));
			pvpClient.startAnimation(fadeInAnimation(pvpClient));
			menu = PVP_MENU;
		}
		else if (v.equals(pvpServeur))
		{
			try
			{
				Intent i = new Intent(this, Game.class);
				i.putExtra("BotVsBot", false);
				i.putExtra("PvP", true);
				i.putExtra("Serveur", true);
				startActivityForResult(i, 0x001);
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		else if (v.equals(pvpClient))
		{
			try
			{
				Intent i = new Intent(this, Game.class);
				i.putExtra("BotVsBot", false);
				i.putExtra("PvP", true);
				i.putExtra("Serveur", false);
				startActivityForResult(i, 0x001);
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		else if (v.equals(playerVsBot))
		{
			try
			{
				Intent i = new Intent(this, Game.class);
				i.putExtra("BotVsBot", false);
				i.putExtra("PvP", false);
				startActivityForResult(i, 0x002);
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		else if (v.equals(botVsBot))
		{
			try
			{
				Intent i = new Intent(this, Game.class);
				i.putExtra("BotVsBot", true);
				i.putExtra("PvP", false);
				startActivity(i);
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		else if (v.equals(shop))
		{
			try
			{
				Intent i = new Intent(this, Shop.class);
				startActivityForResult(i, 0x003);
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		else if (v.equals(cards))
		{
			try
			{
				Intent i = new Intent(this, MyCards.class);
				startActivity(i);
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		else if (v.equals(decks))
		{
			try
			{
				Intent i = new Intent(this, MyDecks.class);
				startActivity(i);
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		else if (v.equals(about))
		{
			showAbout();
		}
	}
}